package com.jiyang.jyandroid.test05;

import android.app.Activity;
import android.content.CursorLoader;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.jiyang.jyandroid.BaseActivity;
import com.jiyang.jyandroid.R;

/**
 * Created by jy on 2016/12/2.
 * 查看并获取联系人
 */

public class Five_1Activity extends BaseActivity {
    private TextView tvName;        //显示姓名
    private TextView tvPhone;       //显示电话
    private static final int PICK_CONTACT_FLAG = 1;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.five_1);
        tvName = (TextView) findViewById(R.id.tv_name);
        tvPhone = (TextView) findViewById(R.id.tv_phone);
        //查看联系人列表
        findViewById(R.id.btn_show_contact).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.setAction(Intent.ACTION_GET_CONTENT);    //让用户选择数据，并返回所选数据
                intent.setType("vnd.android.cursor.item/phone");
                startActivityForResult(intent, PICK_CONTACT_FLAG);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PICK_CONTACT_FLAG) {
            if (resultCode == Activity.RESULT_OK) {
                Uri dataUri = data.getData();       //获取用户选择的数据
                CursorLoader cursorLoader = new CursorLoader(this, dataUri, null, null, null, null);
                Cursor cursor = cursorLoader.loadInBackground();
                //如果查询到联系人
                if (cursor.moveToFirst()) {
                    //获取
                    String contactID = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    //
                    String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                    String phone = "此联系人暂无电话";
                    //根据ID获取联系人信息
                    Cursor cursorPhone = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                    + "=" + contactID, null, null);
                    if (cursorPhone != null && cursorPhone.moveToFirst()) {
                        phone = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    }
                    if (cursorPhone != null) {
                        cursorPhone.close();
                    }
                    tvPhone.setText(phone);
                    tvName.setText(name);
                }
                cursor.close();
            }
        }
    }
}
